package com.htjx.mall.admin.controllers;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.htjx.mall.admin.controllers.vo.OrderQuery;
import com.htjx.mall.commons.api.Result;
import com.htjx.mall.commons.models.Order;
import com.htjx.mall.commons.models.enums.ExpressCom;
import com.htjx.mall.commons.models.enums.OrderStatus;
import com.htjx.mall.commons.utils.page.Page;
import com.htjx.mall.services.OrderService;

@Controller
@RequestMapping("/admin/order")
public class _OrderController {
	@Autowired
	private OrderService orderService;

	/****
	 * 分页查询订单
	 * @param productQuery
	 * @param model
	 * @return
	 */
	@RequestMapping("/list")
	public String list(OrderQuery orderQuery, Model model) {
		Page<Order> page = new Page<Order>();
		page.setPageSize(orderQuery.getPageSize());
		page.setPageNo(orderQuery.getPageNo());
		page = orderService.page(orderQuery, page);
		
		model.addAttribute("page", page);
		model.addAttribute("order_status", OrderStatus.values());
		
		return "order/list";
	}
	
	@RequestMapping(value = "/detail/{order_id}", method = RequestMethod.GET)
	public String categoryDetail(@PathVariable("order_id") long id, Model model) {
		Order order = orderService.selectOne(id);
		Result result = null;
		if(order == null) {
			result = new Result(9,"查询商品失败");
		} 
		
		model.addAttribute("result", result);
		model.addAttribute("order", order);
		return "order/detail";
	}
	
	@RequestMapping(value="/update/{id}", method = RequestMethod.GET)
	public String update(@PathVariable("id") long id, Model model) {
		Order order = orderService.selectOne(id);
		model.addAttribute("order", order);
		model.addAttribute("express_com", ExpressCom.values());
		return "order/update";
	}
	
	@RequestMapping(value="/update", method = RequestMethod.POST)
	public String update(Order order, Model model) {
		Order ref = orderService.selectOne(order.getId());
		if(ref == null) {
			Result result = new Result(9, "数据有误");
			model.addAttribute("result", result);
			return "redirect:/admin/order/detail/"+order.getId();
		}
		
		ref.setOrder_status(order.getOrder_status());
		ref.setExpress_com(order.getExpress_com());
		ref.setExpress_no(order.getExpress_no());
		
		orderService.updateOne(ref);
		return "redirect:/admin/order/list";
	}
}
